package com.grandbroad.eim.ui.activities;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.easy.west.westclient.api.search.bean.SearchList;
import com.easy.west.westclient.api.search.event.SearchEvent;
import com.flyco.tablayout.SlidingTabLayout;
import com.grandbroad.eim.R;
import com.grandbroad.eim.adapter.SearchResultPageAdapter;
import com.grandbroad.eim.app.MyApplication;
import com.grandbroad.eim.base.BaseActivity;
import com.grandbroad.eim.utils.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jmessage.support.qiniu.android.utils.StringUtils;

public class SearchActivity extends BaseActivity {


    @BindView(R.id.edit_search)
    EditText editSearch;
    @BindView(R.id.tv_search_cancel)
    TextView tvSearchCancel;
    @BindView(R.id.top_search_bar)
    Toolbar topSearchBar;
    @BindView(R.id.recycler_history_view)
    RecyclerView recyclerHistoryView;
    @BindView(R.id.recycler_hot_search)
    RecyclerView recyclerHotSearch;
    @BindView(R.id.li_search)
    RelativeLayout liSearch;
    @BindView(R.id.slding_result_tab)
    SlidingTabLayout sldingResultTab;
    @BindView(R.id.viewpage_result_pages)
    ViewPager viewpageResultPages;
    @BindView(R.id.li_search_result)
    RelativeLayout liSearchResult;
    @BindView(R.id.pro_bar)
    ProgressBar proBar;
    @BindView(R.id.tv_empty_state)
    TextView tvEmptyState;
    private String inputContent;
    private SearchResultPageAdapter pageAdapter;

    @Override
    protected void initview() {
        //点击取消
        tvSearchCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (liSearchResult.getVisibility() == View.VISIBLE||!TextUtils.isEmpty(editSearch.getText().toString())) {
                    sldingResultTab.setVisibility(View.GONE);
                    proBar.setVisibility(View.VISIBLE);
                    viewpageResultPages.setVisibility(View.GONE);
                    liSearchResult.setVisibility(View.GONE);
                    tvEmptyState.setVisibility(View.GONE);
                    tvSearchCancel.setText("取消");
                    editSearch.setText(null);
                    return;
                }

                finish();

            }
        });
        //点击搜索框
        editSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    //先隐藏键盘
                    ((InputMethodManager) SearchActivity.this.getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(SearchActivity.this.getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

                    //其次再做相应操作
                    inputContent = editSearch.getText().toString();
                    if (StringUtils.isBlank(inputContent)) {
                    } else {
                        //搜索开始
                        MyApplication.getMoleclient().search(inputContent);
                        tvSearchCancel.setText("返回");
                        liSearchResult.setVisibility(View.VISIBLE);
                    }
                }
                return false;
            }
        });
    }

    @Override
    protected void initdata() {
        pageAdapter = new SearchResultPageAdapter(this);
        viewpageResultPages.setAdapter(pageAdapter);
        sldingResultTab.setViewPager(viewpageResultPages);
    }

    @Override
    protected int setlayout() {
        return R.layout.activity_search;
    }

    //这里需要替换recyclerview的适配器，在结果出来后

    /**
     * 获取到搜索结果
     *
     * @param eva
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getSearchResult(SearchEvent eva) {
        if (eva.isOk()) {
            if (eva.getBean().getData()==null)
            {
                tvEmptyState.setVisibility(View.VISIBLE);
            }
            proBar.setVisibility(View.GONE);
            SearchList list = eva.getBean();
            pageAdapter.setListBeanList(list.getData().getLiveroomDTOList());
            pageAdapter.setVodDTOSBeanslist(list.getData().getVodDTOS());
            pageAdapter.setName(inputContent);
            pageAdapter.notifyDataSetChanged();
            sldingResultTab.setVisibility(View.VISIBLE);
            viewpageResultPages.setVisibility(View.VISIBLE);

        } else {
            ToastUtils.show("搜索失败！");
        }
    }

    @Override
    public void onBackPressed() {
        if (liSearchResult.getVisibility() == View.VISIBLE||!TextUtils.isEmpty(editSearch.getText().toString())) {
            sldingResultTab.setVisibility(View.GONE);
            proBar.setVisibility(View.VISIBLE);
            viewpageResultPages.setVisibility(View.GONE);
            liSearchResult.setVisibility(View.GONE);
            tvEmptyState.setVisibility(View.GONE);
            tvSearchCancel.setText("取消");
            editSearch.setText(null);
            return;
        }
        super.onBackPressed();
    }


}
